package com.ruoyi.income.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.income.domain.DistributionIncome;
import com.ruoyi.income.service.IDistributionIncomeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单返佣收入信息Controller
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@RestController
@RequestMapping("/ruoyi/income")
public class DistributionIncomeController extends BaseController
{
    @Autowired
    private IDistributionIncomeService distributionIncomeService;

    /**
     * 查询订单返佣收入信息列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:income:list')")
    @GetMapping("/list")
    public TableDataInfo list(DistributionIncome distributionIncome)
    {
        startPage();
        List<DistributionIncome> list = distributionIncomeService.selectDistributionIncomeList(distributionIncome);
        return getDataTable(list);
    }

    /**
     * 导出订单返佣收入信息列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:income:export')")
    @Log(title = "订单返佣收入信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DistributionIncome distributionIncome)
    {
        List<DistributionIncome> list = distributionIncomeService.selectDistributionIncomeList(distributionIncome);
        ExcelUtil<DistributionIncome> util = new ExcelUtil<DistributionIncome>(DistributionIncome.class);
        util.exportExcel(response, list, "订单返佣收入信息数据");
    }

    /**
     * 获取订单返佣收入信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:income:query')")
    @GetMapping(value = "/{incomeId}")
    public AjaxResult getInfo(@PathVariable("incomeId") Long incomeId)
    {
        return success(distributionIncomeService.selectDistributionIncomeByIncomeId(incomeId));
    }

    /**
     * 新增订单返佣收入信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:income:add')")
    @Log(title = "订单返佣收入信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DistributionIncome distributionIncome)
    {
        try {
            return toAjax(distributionIncomeService.insertDistributionIncome(distributionIncome));
        } catch (IllegalArgumentException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            return error("新增订单返佣收入信息失败：" + e.getMessage());
        }
    }

    /**
     * 修改订单返佣收入信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:income:edit')")
    @Log(title = "订单返佣收入信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DistributionIncome distributionIncome)
    {
        try {
            return toAjax(distributionIncomeService.updateDistributionIncome(distributionIncome));
        } catch (IllegalArgumentException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            return error("修改订单返佣收入信息失败：" + e.getMessage());
        }
    }

    /**
     * 删除订单返佣收入信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:income:remove')")
    @Log(title = "订单返佣收入信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{incomeIds}")
    public AjaxResult remove(@PathVariable Long[] incomeIds)
    {
        try {
            return toAjax(distributionIncomeService.deleteDistributionIncomeByIncomeIds(incomeIds));
        } catch (Exception e) {
            return error("删除订单返佣收入信息失败：" + e.getMessage());
        }
    }

    /**
     * 更新佣金状态
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:income:edit')")
    @Log(title = "订单返佣收入信息", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{incomeId}/{incomeStatus}")
    public AjaxResult updateStatus(@PathVariable Long incomeId, @PathVariable String incomeStatus)
    {
        try {
            return toAjax(distributionIncomeService.updateIncomeStatus(incomeId, incomeStatus));
        } catch (Exception e) {
            return error("更新佣金状态失败：" + e.getMessage());
        }
    }

    /**
     * 根据分销员ID查询收入列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:income:list')")
    @GetMapping("/distributor/{distributorId}")
    public AjaxResult getByDistributorId(@PathVariable Long distributorId)
    {
        try {
            List<DistributionIncome> list = distributionIncomeService.selectIncomeByDistributorId(distributorId);
            return success(list);
        } catch (Exception e) {
            return error("查询分销员收入失败：" + e.getMessage());
        }
    }
}
